/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import dao.DaoCidade;
import java.awt.event.KeyEvent;
import java.util.List;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import modelo.Cidade;
import tabeladados.TabelaDeDados;

/**
 *
 * @author fumio
 */
public class pesquisarCidade extends javax.swing.JDialog {
    private TabelaDeDados tabela = null;
    private DaoCidade daocidade = new DaoCidade();
    /**
     * Creates new form pesquisar
     */
    public pesquisarCidade(java.awt.Frame parent, boolean modal) {
        
        super(parent, modal);
        initComponents();
        atualizarTabela();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        edtPesquisa = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablePesquisa = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        jPanel1.add(edtPesquisa, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 370, -1));

        tablePesquisa.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tablePesquisa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tablePesquisaKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tablePesquisa);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 57, 400, 410));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-410)/2, (screenSize.height-405)/2, 410, 405);
    }// </editor-fold>//GEN-END:initComponents

    private void tablePesquisaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tablePesquisaKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            
            this.dispose();
        }
    }//GEN-LAST:event_tablePesquisaKeyPressed

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField edtPesquisa;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablePesquisa;
    // End of variables declaration//GEN-END:variables

    
    
    
    public final void atualizarTabela() {

        if (tabela == null) {
            tabela = new TabelaDeDados(tablePesquisa);

            tabela.adicionarColuna("Código", 15);
            tabela.adicionarColuna("Cidade", 40);
            tabela.adicionarColuna("UF", 45);
            tablePesquisa.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                @Override
                @SuppressWarnings("empty-statement")
                public void valueChanged(ListSelectionEvent e) {
                    if (e.getValueIsAdjusting()) {
                       

                    }
                    ;
                }
            });

        }


        tabela.removerTodos();

        String select = "select c from Cidade c";
        if (!edtPesquisa.getText().equals("")) {
            select = select + " where c.cidade like '%" + edtPesquisa.getText() + "%'";
        }
        select = select + " order by c.cod_cidade";

        List dados = daocidade.select(select);

        Cidade aux = null;
        for (int i = 0; i < dados.size(); i++) {
            aux = (Cidade) dados.get(i);
            Object[] linha = {aux.getCod_cidade(), aux.getCidade(), aux.getUf()};
            tabela.adicionarLinha(linha);
        }
        tabela.setDados(dados);

    }
    
    public Cidade Seleciona(){
        
        atualizarTabela();
        this.setVisible(true);
        return (Cidade) tabela.getObjetoSelecionado();
 
    }


}
